package com.javastudy.liuhouse.xml_json.json;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class JsonDo {

    @Test
    public void jsonDoTest() throws IOException {
        //加载json文件 以流的方式存在
        InputStream input = getClass().getClassLoader().getResourceAsStream("book.json");
        ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
        //反序列化的时候忽略不存在的JavaBean属性
        mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES , false);
        Book book = mapper.readValue(input, Book.class);
        System.out.println(book.id);
        System.out.println(book.name);
        System.out.println(book.author);
        System.out.println(book.isbn);
        System.out.println(book.pubDate);
        System.out.println(book.price);

        //序列化为JSON
        String json = mapper.writeValueAsString(book);
        System.out.println(json);
    }

}
